package com.wang.consumer.listerners;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@Component
public class RabbitMqListener {
    @RabbitListener(queues = {"simple.queue"})
    public void onMessage(String message) {
        System.out.println("message:"+ message);
        log.info("message:"+ message);
        throw new RuntimeException("消息处理报错");
    }


    @RabbitListener(queues = {"work.queue"})
    public void workQueue1(String message) {
        log.info("message1:{}", message);
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @RabbitListener(queues = {"work.queue"})
    public void workQueue2(String message) {
        log.error("message2......:{}", message);
        try {
            Thread.sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @RabbitListener(queues = {"fanout.queue1"})
    public void fanoutQueue1(String message) {
        log.error("fanoutQueue1Message......:{}", message);
    }

    @RabbitListener(queues = {"fanout.queue2"})
    public void fanoutQueue2(String message) {
        log.error("fanoutQueue2Message......:{}", message);
    }


//    @RabbitListener(queues = {"direct.queue1"})
//    public void directQueue1(String message) {
//        log.info("directQueue1Message......:{}", message);
//    }
//
//    @RabbitListener(queues = {"direct.queue2"})
//    public void directQueue2(String message) {
//        log.error("directQueue2Message......:{}", message);
//    }


    @RabbitListener(queues = {"topic.queue1"})
    public void topicQueue1(String message) {
        log.info("topicQueue1Message--------------:{}", message);
    }

    @RabbitListener(queues = {"topic.queue2"})
    public void topicQueue2(String message) {
        log.info("topicQueue2Message......:{}", message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "direct.queue1", durable = "true"),
            exchange = @Exchange(value = "direct.exchange",type = "topic"),
            key = {"black"}
    ))
    public void directExchange(String msg){
        log.info("收到消息信息为：{}",msg);
    }


    @RabbitListener(queues = {"direct.queue.new"})
    public void directQueueNew(String message) {
        log.info("directQueueNew--------------:{}", message);
    }


    /**
     * 延迟队列 - 交换机声明
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "delay.queue", durable = "true"),
            exchange = @Exchange(value = "delay.exchange",type = "topic", delayed = "true"),
            key = {"delay"}
    ))
    public void delayExchangeAndQueue(String msg){
        log.info("收到消息信息为：{}",msg);
    }


//    @RabbitListener(queues = {"object.queue"})
//    public void objectQueue(Map<String, Object>  message) {
//        log.info("objectQueue--------------:{}", message);
//    }
}
